//
//  ClockView.m
//  LightControl
//
//  Created by CKF on 2025/8/18.
//

#import "ClockView.h"
@interface ClockView ()
@property (nonatomic, assign) CGFloat handAngle;
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, assign) CGPoint centerPoint;
@property (nonatomic, assign) BOOL isDragging;
@end
@implementation ClockView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.handAngle = -M_PI_2;  // 12 点方向
        self.radius = MIN(frame.size.width, frame.size.height) / 2 ;
        self.centerPoint = CGPointMake(frame.size.width / 2, frame.size.height / 2);
        self.isDragging = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 绘制表盘
    [self drawClockFaceWithContext:context];
    // 绘制数字刻度
    [self drawNumberMarksWithContext:context];
    // 绘制指针
    [self drawHandWithContext:context];
}

- (void)drawClockFaceWithContext:(CGContextRef)context {
    // 设置填充颜色
    CGContextSetFillColorWithColor(context, RGB(220, 221, 228).CGColor);
    
    // 绘制圆形
    CGContextAddArc(context, self.centerPoint.x, self.centerPoint.y, self.radius, 0, M_PI * 2, YES);
    CGContextFillPath(context);
}

- (void)drawNumberMarksWithContext:(CGContextRef)context {
    UIFont *font = [UIFont systemFontOfSize:14];
    
//    if (self.selectType == SelectTypeHour) {
        // 绘制小时刻度
        for (int i = 1; i <= 12; i++) {
            CGFloat angle = i * M_PI / 6 - M_PI_2;
            CGFloat x = self.centerPoint.x + (self.radius - 25) * cos(angle);
            CGFloat y = self.centerPoint.y + (self.radius - 25) * sin(angle);
            NSString *number = [NSString stringWithFormat:@"%d", i];
            [number drawAtPoint:CGPointMake(x - 5, y - 7) withAttributes:@{NSFontAttributeName: font}];
        }
//    } else {
//        // 绘制分钟刻度
//        for (int i = 0; i < 60; i++) {
//            if (i % 5 == 0) {
//                CGFloat angle = i * M_PI / 30 - M_PI_2;
//                CGFloat x = self.centerPoint.x + (self.radius - 25) * cos(angle);
//                CGFloat y = self.centerPoint.y + (self.radius - 25) * sin(angle);
//                NSString *number = [NSString stringWithFormat:@"%d", i == 0 ? 60 : i];
//                [number drawAtPoint:CGPointMake(x - 5, y - 7) withAttributes:@{NSFontAttributeName: font}];
//            }
//        }
//    }
}

- (void)drawHandWithContext:(CGContextRef)context {
    CGFloat handLength = self.radius * 0.7;
    CGPoint endPoint = CGPointMake(self.centerPoint.x + handLength * cos(self.handAngle),
                                   self.centerPoint.y + handLength * sin(self.handAngle));

    CGContextSetStrokeColorWithColor(context, RGB(42, 87, 128).CGColor);
    CGContextSetLineWidth(context, 4.0);
    CGContextMoveToPoint(context, self.centerPoint.x, self.centerPoint.y);
    CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
    CGContextStrokePath(context);

    // 绘制指针末端圆形
    CGContextSetFillColorWithColor(context, RGB(42, 87, 128).CGColor);
    CGContextAddArc(context, endPoint.x, endPoint.y, 5, 0, 2 * M_PI, 0);
    CGContextFillPath(context);
}

- (BOOL)isPointNearHand:(CGPoint)point {
    CGFloat handLength = self.radius * 0.7;
    CGPoint handEndPoint = CGPointMake(self.centerPoint.x + handLength * cos(self.handAngle),
                                       self.centerPoint.y + handLength * sin(self.handAngle));
    CGFloat distance = sqrt(pow(point.x - handEndPoint.x, 2) + pow(point.y - handEndPoint.y, 2));
    return distance <= 10;
}

- (void)updateHandToNearestMarkWithAngle:(CGFloat)angle {
    if (self.selectType == SelectTypeHour) {
        // 计算靠近的小时刻度
        NSInteger hour = (NSInteger)((angle + M_PI_2 + M_PI / 12) * 12 / (2 * M_PI));
        hour = (hour + 12) % 12;
        if (hour == 0) hour = 12;
        self.handAngle = (hour * M_PI / 6) - M_PI_2;
    } else {
        // 计算靠近的分钟刻度
        NSInteger minute = (NSInteger)((angle + M_PI_2 + M_PI / 60) * 60 / (2 * M_PI));
        minute = (minute + 60) % 60;
        self.handAngle = (minute * M_PI / 30) - M_PI_2;
    }

    // 计算时间
    NSInteger hour = 0;
    NSInteger minute = 0;
    if (self.selectType == SelectTypeHour) {
        hour = (NSInteger)((self.handAngle + M_PI_2) * 12 / (2 * M_PI));
        hour = (hour + 12) % 12;
        if (hour == 0) hour = 12;
        minute = 0;
    } else {
        minute = (NSInteger)((self.handAngle + M_PI_2) * 60 / (2 * M_PI));
        minute = (minute + 60) % 60;
        hour = 12; // 假设小时默认 12 点，可按需修改
    }

    if (self.timeChangedBlock) {
        self.timeChangedBlock(hour, minute);
    }

    [self setNeedsDisplay];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    if ([self isPointNearHand:point]) {
        self.isDragging = YES;
    } else {
        CGFloat dx = point.x - self.centerPoint.x;
        CGFloat dy = point.y - self.centerPoint.y;
        CGFloat angle = atan2(dy, dx);
        [self updateHandToNearestMarkWithAngle:angle];
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (!self.isDragging) return;

    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    CGFloat dx = point.x - self.centerPoint.x;
    CGFloat dy = point.y - self.centerPoint.y;
    self.handAngle = atan2(dy, dx);

    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.isDragging) {
        self.isDragging = NO;
        [self updateHandToNearestMarkWithAngle:self.handAngle];
    }
}

@end
